package com.example.demo.controller;

import com.example.demo.entity.CommonResult;
import com.example.demo.entity.loginUser;
import com.example.demo.service.LoginUserServiceimpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/User", produces = "application/json;charset=utf-8")
public class LoginUserController {

    @Autowired
    LoginUserServiceimpl loginUserMapperlmpl;
    @ResponseBody  // 把返回的结果给浏览器
    @PostMapping("/login")
    @ApiOperation(notes = "登录", value = "login")
    public CommonResult Userlogin(@ApiParam(name = "登录信息,登录的时候无需token请求头",required = true) @RequestBody loginUser loginUser){
        String Login=loginUser.getLogin_name().trim();
        String Password=loginUser.getPassword().trim();
        String role=loginUser.getRole().trim();
        CommonResult commonResult = loginUserMapperlmpl.login_authentication(Login, Password, role);
        return commonResult;
    }

    @ResponseBody
    @GetMapping("/get_user_Onepage")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "第几页，不传递默认第一页", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "一页的数据量，默认10条数据 ",  dataType = "Integer", paramType = "query")})
    @ApiOperation(notes = "用户管理，获取用户列表", value = "get_user_Onepage")
    public CommonResult getOnepage( @RequestParam (value ="pageNum", defaultValue = "1")Integer pageNum,
                                    @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,String role){
        if (!role.equals("1")){
            return new CommonResult(201,"权限不够");
        }
        CommonResult commonResult = loginUserMapperlmpl.GetOnepagesUser(pageNum, pageSize);

        return commonResult;
    }

    @ResponseBody  // 把返回的结果给浏览器
    @PostMapping("/addUser")
    @ApiOperation(notes = "添加用户账号", value = "addUser")
    public CommonResult addUser(@ApiParam(name = "用户信息",required = true) @RequestBody loginUser loginUser){
        CommonResult commonResult = loginUserMapperlmpl.addUser(loginUser);
        return commonResult;
    }


    @ResponseBody  // 把返回的结果给浏览器
    @PostMapping("/updateUser")
    @ApiOperation(notes = "添加用户", value = "updateUser")
    public CommonResult updateUser(@ApiParam(name = "用户信息",required = true) @RequestBody loginUser loginUser){
        CommonResult commonResult = loginUserMapperlmpl.updateUser(loginUser);
        return commonResult;
    }

    @ResponseBody  // 把返回的结果给浏览器
    @DeleteMapping("/deleteUser")
    @ApiOperation(notes = "删除用户", value = "deleteUser")
    public CommonResult deleteUser(@RequestParam(value="id")Integer id){
        CommonResult commonResult = loginUserMapperlmpl.deleteUser(id);
        return commonResult;
    }



    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "login_name",value = "用户名")}
    )
    @GetMapping("/selectUser")
    @ApiOperation(notes = "根据ID和名字查询", value = "selectUser")
    public CommonResult selectUser(Integer id,  String login_name) {
        System.out.println(id);
        System.out.println(login_name);
        if (id==null && login_name==null){
            return new CommonResult(401,"不能空参查询");
        }
        CommonResult commonResult = loginUserMapperlmpl.selectUser(id, login_name);
        return commonResult;
    }
}
